/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.Constants;
import com.click.app.DateFormatChanger;
import com.click.app.Functions;
import com.click.app.model.BalanceEmployee;
import com.click.app.model.Employee;
import com.click.app.model.Material;
import com.click.app.model.PantDetail;
import com.click.app.model.PeriodPaymentEmployee;
import com.click.app.model.ProcessDressmaker;
import com.click.app.model.ProcessDressmakerPurchaseDetail;
import com.click.app.model.StorageCut;
import com.click.app.model.StorageMaterial;
import com.click.app.model.TransactionUser;
import com.click.app.service.BalanceEmployeeManager;
import com.click.app.service.CutDetailManager;
import com.click.app.service.EmployeeManager;
import com.click.app.service.PantDetailCutDetailManager;
import com.click.app.service.PantDetailManager;
import com.click.app.service.PeriodPaymentEmployeeManager;
import com.click.app.service.ProcessDressmakerManager;
import com.click.app.service.ProcessDressmakerPurchaseDetailManager;
import com.click.app.service.PurchaseDetailManager;
import com.click.app.service.StorageCutManager;
import com.click.app.service.StorageMaterialManager;
import com.click.app.service.TransactionUserManager;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 *
 * @author ronald
 */
public class ProcessDressMakerAction extends BaseAction {

    private static final long serialVersionUID = -4037514607101222025L;
    ProcessDressmakerManager processDressmakerManager;
    PantDetailCutDetailManager pantDetailCutDetailManager;
    EmployeeManager employeeManager;
    CutDetailManager cutDetailManager;
    List<PantDetail> pantDetailList;
    ProcessDressmaker processDressmaker;
    List<ProcessDressmaker> processDressmakerList;
    List<Employee> employeesList;
    private Long id;
    private StorageCutManager storageCutManager;
    private List<StorageCut> storageCutList;
    private PurchaseDetailManager purchaseDetailManager;
    private List<StorageMaterial> storageMaterialList;
    private StorageMaterialManager storageMaterialManager;
    private List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail;
    private ProcessDressmakerPurchaseDetailManager processDressmakerPurchaseDetailManager;
    private ProcessDressmakerPurchaseDetail processDressmakerPurchaseDetail;
    private PantDetail pantDetail;
    private PantDetailManager pantDetailManager;
    private BalanceEmployeeManager balanceEmployeeManager;
    private String intDateProcessDMaker;
    private String endDateProcessDressmaker;
    private PeriodPaymentEmployeeManager periodPaymentEmployeeManager;
    private PeriodPaymentEmployee periodPaymentEmployee;
    private List<ProcessDressmaker> listAsignDressMaker;
    private Map reportParams;
    private TransactionUserManager transactionUserManager;

    public String list() {
        getSession().removeAttribute("pantDetailList");
        processDressmakerList = processDressmakerManager.getProcessDressmakers();
        return SUCCESS;
    }

    public String listReview() {
        processDressmakerList = processDressmakerManager.getProcessDressmakers();
        return SUCCESS;
    }

    public String save() {
        //  pantDetailList  =  (List<PantDetail>) getSession().getAttribute("pantDetailList");
        if (processDressmaker.getEmployee().getId() != -1 && intDateProcessDMaker != null) {

            Employee employee = employeeManager.get(processDressmaker.getEmployee().getId());
            processDressmaker.setInitDate(DateFormatChanger.changeStringtoDate(intDateProcessDMaker));
            processDressmaker.setEmployee(employee);
            processDressmaker.setEndDate(processDressmaker.getInitDate());
            processDressmaker.setTotalAmount(0D);
            processDressmaker.setTotalAmountFail(0D);
            processDressmaker.setTotalCurrentAmount(0D);
            processDressmaker.setDeleted(true);
            processDressmaker = processDressmakerManager.save(processDressmaker);

            periodPaymentEmployee = periodPaymentEmployeeManager.getCurrentPeriodByIdEmployeeAndRole(employee.getId(), employee.getRoleByName("ROLE_EMPLOYEE_DRESSMAKER").getIdRole());
            BalanceEmployee balanceEmployee = new BalanceEmployee();
            balanceEmployee.setBalance(periodPaymentEmployee.getCurrentAmount());
            balanceEmployee.setAmountPaid(0D);
            balanceEmployee.setAmountWon(0D);
            balanceEmployee.setProcessDressmaker(processDressmaker);
            balanceEmployee.setDateBalance(processDressmaker.getInitDate());
            balanceEmployee.setPeriodPaymentEmployee(periodPaymentEmployee);
            balanceEmployee.setPaymentsCut(null);
            balanceEmployee.setPaymentsPant(null);
            balanceEmployee = balanceEmployeeManager.save(balanceEmployee);
            processDressmaker.setBalanceEmployee(balanceEmployee);
            balanceEmployeeManager.save(balanceEmployee);

            getSession().setAttribute("pDressMId", processDressmaker.getId());
            storageCutList = storageCutManager.getAllGreaterThanCero();
            saveMessage(getText("processDressMakerAction.saveProcessDressMaker"));
            return SUCCESS;

        } else {
            errorMessage(getText("processDressMakerAction.errorProcessDressMaker"));
            return ERROR;
        }
    }

    public String generateJasperAsignDressMaker() {
        System.out.println("ProcessDressMakerAction -> generateJasperAsignDressMaker() ");
        Long pDressMId = (Long) getSession().getAttribute("pDressMId");
        processDressmaker = processDressmakerManager.get(pDressMId);
        processDressmakerList = new ArrayList<ProcessDressmaker>();
        processDressmakerList.add(processDressmaker);

        String pathSubReport = getRequest().getSession().getServletContext().getRealPath("/reports/asignDressMakerSubReport.jasper");
        try {
            JasperReport subReportProcessDressMaker = (JasperReport) JRLoader.loadObject(new File(pathSubReport));
            TransactionUser transaction = new TransactionUser();
            transaction = transactionUserManager.save(transaction);

            String codeTransaction = transaction.getId().toString();
            String date = processDressmaker.getInitDate().toString() + "##" + processDressmaker.getEndDate().toString();
            String nameTransaction = "ASING PROCESS DRESS MAKER";
            String code = codeTransaction + "##" + date + "##" + nameTransaction;
            String newCodeTransaction = Functions.getCodePrintTransaction(code);

            reportParams = new HashMap();
            reportParams.put("subReportProcessDressMaker", subReportProcessDressMaker);
            reportParams.put("labelCodeTransaction", getText("codeTransaction"));
            reportParams.put("codeTransaction", newCodeTransaction);
            reportParams.put("labelTitle", getText("processDressmaker.title"));

            reportParams.put("sLabelTitle", getText("addDressMakerForm.itemsAsigned"));
            reportParams.put("sLabelDescriptionMaterial", getText("storageCut.pantDesign.fullData"));
            reportParams.put("sLabelDistribution", getText("formProcessDressMaker.distribution"));
            reportParams.put("sLabelQuantity", getText("pantDetail.quantity"));

            reportParams.put("labelNameEmployee", getText("nameEmployee"));
            reportParams.put("nameEmployee", processDressmaker.getEmployee().getFullName());
            reportParams.put("labelDate", getText("date"));
            reportParams.put("date", processDressmaker.getInitDate().toString() + " - " + processDressmaker.getEndDate().toString());

        } catch (JRException ex) {
            Logger.getLogger(PurchaseDetailAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("ProcessDressMakerAction -> end generateJasperAsignDressMaker() ");
        return SUCCESS;
    }

    public String generateJasperAsignDressMakerReview() {
        System.out.println("ProcessDressMakerAction -> generateJasperAsignDressMakerReview()");
        Long pDMid = (Long)getSession().getAttribute("pDMid");
        processDressmaker = processDressmakerManager.get(pDMid);
        processDressmakerList = new ArrayList<ProcessDressmaker>();
        processDressmakerList.add(processDressmaker);
        //processDressmaker.getPantDetails()
        String pathSubReport = getRequest().getSession().getServletContext().getRealPath("/reports/processDressMakerReviewSR.jasper");
        try {
              JasperReport processDressMakerReviewSR = (JasperReport) JRLoader.loadObject(new File(pathSubReport));
              TransactionUser transaction = new TransactionUser();
              transaction = transactionUserManager.save(transaction);

              String codeTransaction = transaction.getId().toString();
              String date = processDressmaker.getInitDate().toString()+"##"+processDressmaker.getEndDate().toString();
              String nameTransaction = "ASING PROCESS DRESSMAKER REVIEW";
              String code = codeTransaction+"##"+date+"##"+nameTransaction;
              String newCodeTransaction = Functions.getCodePrintTransaction(code);

              reportParams = new HashMap();
              reportParams.put("sRProcessDressMakerReview", processDressMakerReviewSR);
              reportParams.put("labelCodeTransaction", getText("codeTransaction"));
              reportParams.put("codeTransaction", newCodeTransaction);
              reportParams.put("labelTitle", getText("cutProcess.title"));

              reportParams.put("labelTotalAmount", getText("cutProcessForm.asignationDetail.totalAmount"));
              reportParams.put("totalAmount", processDressmaker.getTotalAmount().toString());
              reportParams.put("labelTotalAmountFail", getText("cutProcess.totalAmountFail"));
              reportParams.put("totalAmountFail", processDressmaker.getTotalAmountFail().toString());
              reportParams.put("labelTotalRealAmount", getText("processDressmaker.totalCurrentAmount"));
              reportParams.put("totalRealAmount", processDressmaker.getTotalCurrentAmount().toString());

              reportParams.put("sLabelTitle", getText("addDressMakerForm.itemsAsigned"));
              reportParams.put("sLabelFullData", getText("storageCut.pantDesign.fullData"));
              reportParams.put("sLabelQuantity", getText("formProcessDressMaker.quantity"));
              reportParams.put("sLabelPriceXclothes", getText("formProcessDressMaker.priceXclothes"));
              reportParams.put("sLabelQuantityFail", getText("formProcessDressMaker.quantityFail"));
              reportParams.put("sLabelPriceXclotheFail", getText("formProcessDressMaker.priceXquantityFail"));
              reportParams.put("sLabelTotalAmount", getText("formProcessDressMaker.totalAmount"));
              reportParams.put("sLabelQuantityRealClothes", getText("formProcessDressMaker.quantityRealClothes"));
              reportParams.put("sLabelDistribution", getText("formProcessDressMaker.distribution"));

              reportParams.put("labelNameEmployee", getText("nameEmployee"));
              reportParams.put("nameEmployee", processDressmaker.getEmployee().getFullName());
              reportParams.put("labelDate", getText("date"));
              reportParams.put("date", processDressmaker.getInitDate().toString()+" - "+processDressmaker.getEndDate().toString());

        } catch (JRException ex) {
            Logger.getLogger(PurchaseDetailAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("ProcessDressMakerAction -> end generateJasperAsignDressMakerReview()");
        return SUCCESS;
    }

    public String saveProcessDressmakerReview() {
        if (endDateProcessDressmaker != null) {
            ProcessDressmaker processDressmakerAux = processDressmakerManager.get(processDressmaker.getId());
            processDressmakerAux.setEndDate(DateFormatChanger.changeStringtoDate(endDateProcessDressmaker));
            processDressmakerManager.save(processDressmakerAux);
        }
        return INPUT;
    }

    public String cancelProcessDressmakerReview() {
        getSession().removeAttribute("pDressMId");
        return INPUT;
    }

    public String addMaterialUsedDressMaker() {
        System.out.println("addMaterialDressmakerUsed() ");
        List<Material> materials = purchaseDetailManager.findAllNoPackageDetail();
        storageMaterialList = new ArrayList<StorageMaterial>();
        for (Iterator<Material> it = materials.iterator(); it.hasNext();) {
            Material material = it.next();
            storageMaterialList.addAll(storageMaterialManager.findByIdMaterialNoCero(material.getId()));
        }
        processDressmaker = processDressmakerManager.get((Long) getSession().getAttribute("pDMid"));
        listProcessDressmakerPurchaseDetail = processDressmaker.getListProcessDressmakerPurchaseDetail();
        System.out.println("end addMaterialDressmakerUsed() ");
        return SUCCESS;
    }

    public String cancel() {
        getSession().removeAttribute("pantDetailList");

        return CANCEL;
    }

    public String edit() {
        // pantDetailList = (List<PantDetail>) getSession().getAttribute("pantDetailList");
        if (id != null) {
            processDressmaker = processDressmakerManager.get(id);
            pantDetailList = processDressmaker.getPantDetails();

        } else {
            if (pantDetailList == null) {
                pantDetailList = new ArrayList<PantDetail>();
            }
            processDressmaker = new ProcessDressmaker();
        }
        employeesList = employeeManager.findByRole(Constants.ROLE_EMPLOYEE_DRESSMAKER);
        getSession().setAttribute("processDressmaker", processDressmaker);
        getSession().setAttribute("pantDetailList", pantDetailList);

        return SUCCESS;
    }

    public String addPantDetailProcessDressmaker() {
        if (id == null) {
            if (getSession().getAttribute("pDressMId") != null) {
                id = (Long) getSession().getAttribute("pDressMId");
            }
        }

        processDressmaker = processDressmakerManager.get(id);
        storageCutList = storageCutManager.getAllGreaterThanCero();
        pantDetailList = processDressmaker.getPantDetails();
        getSession().setAttribute("pDressMId", processDressmaker.getId());
        return SUCCESS;
    }

    public String delete() {
        System.out.println("ProcessDressMakerAction-> delete()");
        if (id != null) {
            processDressmaker = processDressmakerManager.get(id);
            if (processDressmaker.getDeleted()) {
                processDressmakerManager.remove(id);
                saveMessage(getText("processDressMakerAction.deleteProcessDressMaker"));
            } else {
                errorMessage(getText("processDressMakerAction.errorDeleteProcessDressMaker"));
            }
        }
        System.out.println("ProcessDressMakerAction-> end delete()");
        return SUCCESS;
    }

    public String reasignPantDetailProcessDressmaker() {
        if (id != null) {
            pantDetail = pantDetailManager.get(id);
            if (pantDetail.getDeleted()) {
                processDressmakerList = processDressmakerManager.getAll();
                return SUCCESS;
            }
        }
        return INPUT;
    }

    public String saveReAsignationPantDetail() {
        if (processDressmaker.getId() > -1) {
            pantDetail = pantDetailManager.get(pantDetail.getId());
            processDressmaker = processDressmakerManager.get(processDressmaker.getId());
            pantDetail.setProcessDressmaker(processDressmaker);
            pantDetailManager.save(pantDetail);
        }
        return INPUT;
    }

    public String editReview() {
        System.out.println("ProcessDressMakerAction-> editReview() ");
        if (id == null) {
            if (getSession().getAttribute("pDMid") != null) {
                id = (Long) getSession().getAttribute("pDMid");
            }
        }

        if (id != null) {
            processDressmaker = processDressmakerManager.get(id);
            endDateProcessDressmaker = DateFormatChanger.changeDatetoString(processDressmaker.getEndDate());
            pantDetailList = processDressmaker.getPantDetails();

            listProcessDressmakerPurchaseDetail = processDressmaker.getListProcessDressmakerPurchaseDetail();
            getSession().setAttribute("pDMid", processDressmaker.getId());
        }
        System.out.println("ProcessDressMakerAction-> end editReview() ");
        return SUCCESS;
    }

    public String editMaterialUsedProcessDressmaker() {
        System.out.println("editMaterialUsedProcessDressmaker() " + id);
        processDressmakerPurchaseDetail = processDressmakerPurchaseDetailManager.get(id);
        System.out.println("end editMaterialUsedProcessDressmaker() ");
        return SUCCESS;
    }

    public ProcessDressmaker getProcessDressmaker() {
        return processDressmaker;
    }

    public void setProcessDressmaker(ProcessDressmaker processDressmaker) {
        this.processDressmaker = processDressmaker;
    }

    public ProcessDressmakerManager getProcessDressmakerManager() {
        return processDressmakerManager;
    }

    public void setProcessDressmakerManager(ProcessDressmakerManager processDressmakerManager) {
        this.processDressmakerManager = processDressmakerManager;
    }

    public CutDetailManager getCutDetailManager() {
        return cutDetailManager;
    }

    public void setCutDetailManager(CutDetailManager cutDetailManager) {
        this.cutDetailManager = cutDetailManager;
    }

    public List<PantDetail> getPantDetailList() {
        return pantDetailList;
    }

    public void setPantDetailList(List<PantDetail> pantDetailList) {
        this.pantDetailList = pantDetailList;
    }

    public List<ProcessDressmaker> getProcessDressmakerList() {
        return processDressmakerList;
    }

    public void setProcessDressmakerList(List<ProcessDressmaker> processDressmakerList) {
        this.processDressmakerList = processDressmakerList;
    }

    public List<Employee> getEmployeesList() {
        return employeesList;
    }

    public void setEmployeesList(List<Employee> employeesList) {
        this.employeesList = employeesList;
    }

    public EmployeeManager getEmployeeManager() {
        return employeeManager;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public PantDetailCutDetailManager getPantDetailCutDetailManager() {
        return pantDetailCutDetailManager;
    }

    public void setPantDetailCutDetailManager(PantDetailCutDetailManager pantDetailCutDetailManager) {
        this.pantDetailCutDetailManager = pantDetailCutDetailManager;
    }

    public StorageCutManager getStorageCutManager() {
        return storageCutManager;
    }

    public void setStorageCutManager(StorageCutManager storageCutManager) {
        this.storageCutManager = storageCutManager;
    }

    public List<StorageCut> getStorageCutList() {

        return storageCutList;
    }

    public void setStorageCutList(List<StorageCut> storageCutList) {
        this.storageCutList = storageCutList;
    }

    public PurchaseDetailManager getPurchaseDetailManager() {
        return purchaseDetailManager;
    }

    public void setPurchaseDetailManager(PurchaseDetailManager purchaseDetailManager) {
        this.purchaseDetailManager = purchaseDetailManager;
    }

    public List<StorageMaterial> getStorageMaterialList() {
        return storageMaterialList;
    }

    public void setStorageMaterialList(List<StorageMaterial> storageMaterialList) {
        this.storageMaterialList = storageMaterialList;
    }

    public StorageMaterialManager getStorageMaterialManager() {
        return storageMaterialManager;
    }

    public void setStorageMaterialManager(StorageMaterialManager storageMaterialManager) {
        this.storageMaterialManager = storageMaterialManager;
    }

    public List<ProcessDressmakerPurchaseDetail> getListProcessDressmakerPurchaseDetail() {
        return listProcessDressmakerPurchaseDetail;
    }

    public void setListProcessDressmakerPurchaseDetail(List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail) {
        this.listProcessDressmakerPurchaseDetail = listProcessDressmakerPurchaseDetail;
    }

    public PantDetail getPantDetail() {
        return pantDetail;
    }

    public void setPantDetail(PantDetail pantDetail) {
        this.pantDetail = pantDetail;
    }

    public PantDetailManager getPantDetailManager() {
        return pantDetailManager;
    }

    public void setPantDetailManager(PantDetailManager pantDetailManager) {
        this.pantDetailManager = pantDetailManager;
    }

    public ProcessDressmakerPurchaseDetailManager getProcessDressmakerPurchaseDetailManager() {
        return processDressmakerPurchaseDetailManager;
    }

    public void setProcessDressmakerPurchaseDetailManager(ProcessDressmakerPurchaseDetailManager processDressmakerPurchaseDetailManager) {
        this.processDressmakerPurchaseDetailManager = processDressmakerPurchaseDetailManager;
    }

    public ProcessDressmakerPurchaseDetail getProcessDressmakerPurchaseDetail() {
        return processDressmakerPurchaseDetail;
    }

    public void setProcessDressmakerPurchaseDetail(ProcessDressmakerPurchaseDetail processDressmakerPurchaseDetail) {
        this.processDressmakerPurchaseDetail = processDressmakerPurchaseDetail;
    }

    public BalanceEmployeeManager getBalanceEmployeeManager() {
        return balanceEmployeeManager;
    }

    public void setBalanceEmployeeManager(BalanceEmployeeManager balanceEmployeeManager) {
        this.balanceEmployeeManager = balanceEmployeeManager;
    }

    public String getIntDateProcessDMaker() {
        return intDateProcessDMaker;
    }

    public void setIntDateProcessDMaker(String intDateProcessDMaker) {
        this.intDateProcessDMaker = intDateProcessDMaker;
    }

    public String getEndDateProcessDressmaker() {
        return endDateProcessDressmaker;
    }

    public void setEndDateProcessDressmaker(String endDateProcessDressmaker) {
        this.endDateProcessDressmaker = endDateProcessDressmaker;
    }

    public PeriodPaymentEmployee getPeriodPaymentEmployee() {
        return periodPaymentEmployee;
    }

    public void setPeriodPaymentEmployee(PeriodPaymentEmployee periodPaymentEmployee) {
        this.periodPaymentEmployee = periodPaymentEmployee;
    }

    public PeriodPaymentEmployeeManager getPeriodPaymentEmployeeManager() {
        return periodPaymentEmployeeManager;
    }

    public void setPeriodPaymentEmployeeManager(PeriodPaymentEmployeeManager periodPaymentEmployeeManager) {
        this.periodPaymentEmployeeManager = periodPaymentEmployeeManager;
    }

    public Map getReportParams() {
        return reportParams;
    }

    public void setReportParams(Map reportParams) {
        this.reportParams = reportParams;
    }

    public TransactionUserManager getTransactionUserManager() {
        return transactionUserManager;
    }

    public void setTransactionUserManager(TransactionUserManager transactionUserManager) {
        this.transactionUserManager = transactionUserManager;
    }
}
